﻿using AiMoBridge.Interface;
using AiMoWindows.Localization;
using AiMoWindows.MainActions.LibraryActions;
using System.Windows;
using System.Windows.Input;

namespace AiMoWindows.Windows.CommandLibrary.ViewModels
{
    class CommandItemViewModel : ChangeLanguageViewModelBase
    {
        public delegate void RemoveComandItemHander(CommandItemViewModel current);
        public event RemoveComandItemHander? RemoveCurrent;
        public ScriptsLibraryItem OwnerItem { get; }
        public bool IsDeleted { get; private set; } = false;
        readonly IDebugPrinter printer;
        public CommandItemViewModel(IDebugPrinter printer, ScriptsLibraryItem ownerItem)
        {
            this.printer = printer;
            OwnerItem = ownerItem;
            Name = ownerItem.Name;
            ExportCommandEnglishName = ownerItem.CommandEnglishName;

            RenameCommmand = new RelayCommand(RenameItem);
            DeleteCommmand = new RelayCommand(DeleteItem);
            SetCommandNameCommand = new RelayCommand(SetCommandName);
            ClearCommandNameCommand = new RelayCommand(ClearCommandName);
            EditCommand = new RelayCommand(EditItem);
        }

        #region ContextMenuActions

        public string RenameText => IsEnglish ? "Rename Item" : "重命名代码";
        public string DeleteText => IsEnglish ? "Delete Item" : "删除代码";
        public string SetCommandNameText => IsEnglish ? "Export Item To Rhp" : "设置代码导出Rhp";
        public string ClearCommandNameText => IsEnglish ? "Remove From Export Rhp" : "取消代码导出";
        public string EditText => IsEnglish ? "Edit Item" : "编辑代码";
        public ICommand RenameCommmand { get; }
        public ICommand DeleteCommmand { get; }
        public ICommand SetCommandNameCommand { get; }
        public ICommand ClearCommandNameCommand { get; }
        public ICommand EditCommand { get; }
        void RenameItem()
        {
            InputString.InputStringDialog inputStringDialog = new InputString.InputStringDialog
            (
                titleEN: "Rename Scipt Item",
                titleCN: "重命名项目",
                labelEN: "Rename without including special characters.",
                labelCN: "重命名，不要包含特殊符号",
                defaultText: Name
            );
            inputStringDialog.ShowDialog();
            string newName = inputStringDialog.UserInputString;
            if (string.IsNullOrEmpty(newName)) return;
            if (!OwnerItem.Rename(newName, out string error))
            {
                printer.PrintDebug(error);
            }
            else
            {
                Name = OwnerItem.Name;
            }
        }
        void DeleteItem()
        {
            if (!OwnerItem.Delete(out string error))
            {
                printer.PrintDebug(error);
                return;
            }
            RemoveCurrent?.Invoke(this);
        }
        bool IsValidChar(char c)
        {
            int i = c;
            return (i > 47 && i < 58) || (i > 96 && i < 123) || (i > 64 && i < 91);
        }
        void SetCommandName()
        {
            InputString.InputStringDialog inputStringDialog = new InputString.InputStringDialog
            (
                titleEN: "Rhino Command EnglishName",
                titleCN: "Rhino 命令英文名",
                labelEN: "Set the Command English Name of export rhp in Rhino, only contains: a-z/A-Z/0-9",
                labelCN: "设置导出插件中，这个命令的英文名，只包含字母和数字",
                defaultText: ExportCommandEnglishName
            );
            if (inputStringDialog.ShowDialog() != true) return;
            string newName = inputStringDialog.UserInputString;
            if (string.IsNullOrEmpty(newName))
            {
                printer.PrintDebug(Local.Get("Input name cannot be empty"));
                return;
            }
            foreach (char c in newName)
            {
                if (!IsValidChar(c))
                {
                    printer.PrintDebug(Local.Get("Input name format error, only contains: a-z/A-Z/0-9"));
                    return;
                }
            }
            if (!OwnerItem.ChangeCommandName(newName, out string error))
            {
                printer.PrintDebug(error);
            }
            ExportCommandEnglishName = OwnerItem.CommandEnglishName;
        }

        void ClearCommandName()
        {
            if (!OwnerItem.ChangeCommandName("", out string error))
            {
                printer.PrintDebug(error);
            }
            ExportCommandEnglishName = OwnerItem.CommandEnglishName;
        }

        void EditItem()
        {
            Application.Current.Dispatcher.InvokeAsync(() =>
            {
                if (!OwnerItem.SendToEdit(out string error))
                {
                    printer.PrintDebug(error);
                }
            });
        }

        #endregion
        #region Display Text
        string _Name = "";
        public string Name
        {
            get => _Name;
            set
            {
                _Name = value;
                RaisePropertyChanged(nameof(Name));
            }
        }
        public string ShowedEnglishName => string.IsNullOrEmpty(ExportCommandEnglishName) ?
            (IsEnglish ? "No Export" : "不导出") : ExportCommandEnglishName;
        string _ExportCommandEnglishName = "";
        public string ExportCommandEnglishName
        {
            get => _ExportCommandEnglishName;
            set
            {
                _ExportCommandEnglishName = value;
                RaisePropertyChanged(nameof(ShowedEnglishName));
            }
        }
        #endregion
    }
}
